package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.reconciliation.FindReconciliationReception;
import com.roc.steelend.controller.vo.reconciliation.ReconciliationBuyReception;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.mapper.BuyOrderMapper;
import com.roc.steelend.mapper.ReconciliationBuyMapper;
import com.roc.steelend.mapper.SuppliersMapper;
import com.roc.steelend.pojo.ReconciliationBuy;
import com.roc.steelend.pojo.Suppliers;
import com.roc.steelend.service.ReconciliationBuyService;
import com.roc.steelend.util.LshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class ReconciliationBuyServiceImpl  implements ReconciliationBuyService {

    @Autowired
    private ReconciliationBuyMapper reconciliationBuyMapper;
    @Autowired
    private BuyOrderMapper buyOrderMapper;
    @Autowired
    private SuppliersMapper suppliersMapper;

    @Override
    public ReturnMsg postReconciliationBuy(ReconciliationBuyReception reconciliationBuyReception,Integer employeeId) {

//        生成调账的数据采购
        ReturnMsg msg = new ReturnMsg();
        ReconciliationBuy reconciliationBuy = new ReconciliationBuy();
        BigInteger reconciliationBuyLsh= LshUtil.getReconciliationBuy();

        reconciliationBuy.setReconciliationBuyId(reconciliationBuyLsh);
        reconciliationBuy.setReconciliationBuyTime(reconciliationBuyReception.getReconciliationTime());
        reconciliationBuy.setEmployeeId(employeeId);
        reconciliationBuy.setOriginallyMoney(reconciliationBuyReception.getOldDebt());
        reconciliationBuy.setNowMoney(reconciliationBuyReception.getNewDebt());
        reconciliationBuy.setOperateTime(new Timestamp(new Date().getTime()));
        reconciliationBuy.setSuppliersId(reconciliationBuyReception.getSupplierId());
        if (Objects.equals(reconciliationBuyReception.getRemark(), "")){
            reconciliationBuyReception.setRemark(null);
        }
        reconciliationBuy.setRemark(reconciliationBuyReception.getRemark());
        int insert = reconciliationBuyMapper.insert(reconciliationBuy);
        if (insert==0){
            msg.setMsg("调账操作失败，请重试");
            msg.setCode(0);
            return  msg;
        }

//        更新供应商的金额数据
        QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
            suppliersQueryWrapper.eq("supplier_id",reconciliationBuyReception.getSupplierId());
        Suppliers suppliers = suppliersMapper.selectList(suppliersQueryWrapper).get(0);
        suppliers.setDebt(reconciliationBuyReception.getNewDebt());
        suppliersMapper.updateById(suppliers);

//        操作采购订单
        buyOrderMapper.updateReconciliationBySuppliers(reconciliationBuyReception.getSupplierId(), reconciliationBuyLsh);

        msg.setMsg("调账操作成功");
        msg.setCode(1);
        return msg;
    }

    @Override
    public List<HashMap<String, Object>> findReconciliationDataByBuy(FindReconciliationReception findReconciliationReception) {
        Integer now = findReconciliationReception.getCurrentPage();
        Integer size = findReconciliationReception.getPageSize();
        now=(now-1)*size;
        String duiXiang = findReconciliationReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }


        return reconciliationBuyMapper.findReconciliationDataByBuy(now,size,duiXiang);
    }

    @Override
    public Integer findReconciliationDataByBuyCount(FindReconciliationReception findReconciliationReception) {
        String duiXiang = findReconciliationReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }
        return reconciliationBuyMapper.findReconciliationDataByBuyCount(duiXiang);
    }

    @Override
    public List<ReconciliationBuy> buyReconciliationDetailSelect(BigInteger reconciliationId) {
        QueryWrapper<ReconciliationBuy> reconciliationBuyQueryWrapper = new QueryWrapper<>();
        reconciliationBuyQueryWrapper.eq("reconciliation_buy_id",reconciliationId);
        return       reconciliationBuyMapper.selectList(reconciliationBuyQueryWrapper);


    }
}
